package com.example.app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.app.database.DBUserInfo;

public class RegisterPage extends AppCompatActivity implements View.OnClickListener{

    private String realCode;
    private DBUserInfo mDBUserInfo;
    private Button mBtRegisterActivityRegister;
    private Button mBtRegisterActivityBack;
    private Button mShow;
    private EditText mEtRegisterActivityUsername;
    private EditText mEtRegisterActivityPassword1;
    private EditText mEtRegisterActivityPassword2;
    private EditText mEtRegisterActivityPhoneCodes;
    private ImageView mIvRegisterActivityShowCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registerpage);

        init();
        mDBUserInfo =new DBUserInfo(this);
        //mIvRegisterActivityShowCode.setImageBitmap(Code.getInstance().createBitmap());
        //realCode = Code.getInstance().getCode().toLowerCase();
    }

    private void init() {
        mBtRegisterActivityRegister = findViewById(R.id.reg_btn);
        mBtRegisterActivityBack = findViewById(R.id.reg_back);
        mEtRegisterActivityUsername = findViewById(R.id.reg_account);
        mEtRegisterActivityPassword1 = findViewById(R.id.reg_password);
        mEtRegisterActivityPassword2 = findViewById(R.id.reg_password2);
        mShow=findViewById(R.id.reg_show);
        //mEtRegisterActivityPhoneCodes = findViewById(R.id.et_registeractivity_phoneCodes);
        //mIvRegisterActivityShowCode = findViewById(R.id.iv_registeractivity_showCode);
        mBtRegisterActivityRegister.setOnClickListener(this);
        mBtRegisterActivityBack.setOnClickListener(this);
        mShow.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.reg_btn:
                String username=mEtRegisterActivityUsername.getText().toString().trim();
                String password1=mEtRegisterActivityPassword1.getText().toString().trim();
                String password2=mEtRegisterActivityPassword2.getText().toString().trim();

                if(!TextUtils.isEmpty(username)&& !TextUtils.isEmpty(password1)&&!TextUtils.isEmpty(password2))
                {
                    if(password1.equals(password2))
                    {
                        mDBUserInfo.add(username,password2);
                        Toast.makeText(this,"已经成功注册啦！",Toast.LENGTH_SHORT).show();
                        finish();
                    }
                    else
                        Toast.makeText(this,"两次密码输入不一致，请重输",Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(this,"请将信息填写完整",Toast.LENGTH_SHORT).show();
                break;
            case R.id.reg_back:
                finish();
                break;
            case R.id.reg_show:
                startActivity(new Intent(this,ShowAllCount.class));
                break;

        }
    }
}